#include<bits/stdc++.h>
#define ll long long
using namespace std;
int dp[2][205][205],n,a[205];
int main(){
	ios::sync_with_stdio(0);
  	freopen("shung.in","r",stdin);
  	freopen("shung.out","w",stdout);
	memset(dp,0x80,sizeof(dp));
	cin>>n;
	dp[0][0][n+1]=0;
	for(int i=1;i<=n;i++){
		memset(dp[i&1],0x80,sizeof(dp[i&1]));
		cin>>a[i];
		for(int l=0;l<=n+1;l++){
			for(int r=0;r<=n+1;r++){
				dp[i&1][l][min(r,a[i])]=max(dp[i&1][l][min(r,a[i])],dp[(i-1)&1][l][r]+(a[i]<r));
				dp[i&1][max(l,a[i])][r]=max(dp[i&1][max(l,a[i])][r],dp[(i-1)&1][l][r]+(a[i]>l));
			}
		}
	}
	int ans=0;
	for(int l=0;l<=n+1;l++)
		for(int r=0;r<=n+1;r++)ans=max(ans,dp[n&1][l][r]);
	cout<<ans;
}
